package com.jzg.jcptadmin.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.Utils.MyToast;
import com.jzg.jcptadmin.Utils.StringUtils;
import com.jzg.jcptadmin.adpter.AppraisersLeftAdapter;
import com.jzg.jcptadmin.adpter.AppraisersRightAdapter;
import com.jzg.jcptadmin.adpter.CityPopAdapter;
import com.jzg.jcptadmin.adpter.PopAdapter;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.AppraisersData;
import com.jzg.jcptadmin.data.vo.CityItem;
import com.jzg.jcptadmin.data.vo.User;
import com.jzg.jcptadmin.presenter.AppraisersPresenter;
import com.jzg.jcptadmin.view.MyListView;
import com.jzg.jcptadmin.view.MySyncHorizontalScrollView;
import com.jzg.jcptadmin.view.SelectDateActivity1;
import com.jzg.jcptadmin.viewinterface.AppraisersInterface;
import com.orhanobut.logger.Logger;
import com.yyydjk.library.DropDownMenu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * author: guochen
 * date: 2016/7/7 10:23
 * email:
 * 评估师统计
 */
public class AppraisersStatisticsActivity extends BaseActivity implements AppraisersInterface {
    @Bind(R.id.dropDownMenu)
    DropDownMenu dropDownMenu;
    @Bind(R.id.title_content)
    TextView titleContent;
    //    private String titles[] = {"今日统计", "全部城市"};
    private String date[] = {"今日统计", "昨日统计", "本周统计", "本月统计"};
    //    private String citys[] = {"郑州", "北京", "上海", "华盛顿"};
    private ListView datelistview;
    private View foot;
    private PopAdapter popAdapter;
    private ListView cityListView;
    private List<View> listViews = new ArrayList();
    private List<CityItem.DataBean> citys = new ArrayList<>();//城市
    private List<AppraisersData.DataBean> AppraisersDatas = new ArrayList<>();//表格
    public HorizontalScrollView mTouchView;
    //装入所有的HScrollView
//    protected List<AppraisersStatisticsCHScrollView2> mHScrollViews = new ArrayList<AppraisersStatisticsCHScrollView2>();
    //定义map集合的key值
    private String[] cols = new String[]{"标题", "列1", "列2", "列3", "列4", "列5", "列6"};
    private View contentView;
    private TextView tv_timestart;
    private TextView tv_timeend;
    private String startDate;
    private String endDate;
    private User user;
    private CityPopAdapter cityPopAdapter;
    private ListView mListView;
    private String DateType = "1";   //查询时间类型：1今日、2昨日、3本周、4本月、5自定义
    private String BeginDate;//时间类型为5时有用，自定义开始时间
    private String EndDate;//时间类型为5时有用，自定义结束时间
    private String CityID = "";
    private AppraisersPresenter appraisersPresenter;
    private CityItem cityItem;
    private String[] titles;
    private List<Map<String, String>> datas = new ArrayList<Map<String, String>>();//存储所有的数据

    private MySyncHorizontalScrollView rightTitleHorscrollView;
    private MyListView contentListViewLeft;//名称这一列的listView
    private MyListView contentListViewRight;  //名称这一列右边的数据listView
    private MySyncHorizontalScrollView rightContentHorscrollView;
    private TextView tvItem;
    private AppraisersLeftAdapter appraisersLeftAdapter;
    private AppraisersRightAdapter appraisersRightAdapter;
    private View cityView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appraisers);
        foot = View.inflate(this, R.layout.order_listfoot, null);
        cityView = View.inflate(this, R.layout.city_view, null);
        cityListView = (ListView) cityView.findViewById(R.id.listview);
        contentView = View.inflate(this, R.layout.activity_appraiserscontentview, null);
        ButterKnife.bind(this);
        titleContent.setText("评估师统计");
//        ViewGroup.LayoutParams params = dropDownMenu.getLayoutParams();
//        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        params.height = ScreenUtils.getScreenWidth(this) * 3 / 4;
//        dropDownMenu.setLayoutParams(params);
        user = appContext.getUser();


//        appraisersPresenter = new AppraisersPresenter(DataManager.getInstance());
//        appraisersPresenter.attachView(this);
//        appraisersPresenter.getCityData(getCityDataParams());
        cityItem = (CityItem) getIntent().getSerializableExtra(Constant.ACTIVITY_CITYITEM);
        if (cityItem != null) {
            citys.addAll(cityItem.getData());
            titles = new String[]{"今日统计", citys.get(0).getCityName()};
            CityID = cityItem.getData().get(0).getCityId() + "";
            initView();
            initListtener();
            initData();
        }

    }

    private void initData() {
        showDialog();
        appraisersPresenter = new AppraisersPresenter(DataManager.getInstance());
        appraisersPresenter.attachView(this);
        appraisersPresenter.getAppraisersData(getappraisersDataParams());
    }

    private void initListtener() {
        datelistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popAdapter.setCheckItem(position);
                dropDownMenu.setTabText(date[position]);
                startDate = "";
                endDate = "";
                tv_timestart.setText("");
                tv_timeend.setText("");
                DateType = position + 1 + "";
                dropDownMenu.closeMenu();
                showDialog();
                appraisersPresenter.getAppraisersData(getappraisersDataParams());
            }
        });

        cityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CityID = citys.get(position).getCityId() + "";
                cityPopAdapter.setCheckItem(position);
                dropDownMenu.setTabText(citys.get(position).getCityName());
                dropDownMenu.closeMenu();
                showDialog();
                appraisersPresenter.getAppraisersData(getappraisersDataParams());
            }
        });

        tv_timestart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choosStartDate(Constant.TIME_START);
            }
        });

        tv_timeend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choosEndDate(Constant.TIME_END);
            }
        });
    }

    private void initView() {
        rightTitleHorscrollView = (MySyncHorizontalScrollView) contentView.findViewById(R.id.rightTitleHorscrollView);
        rightContentHorscrollView = (MySyncHorizontalScrollView) contentView.findViewById(R.id.rightContentHorscrollView);
        contentListViewLeft = (MyListView) contentView.findViewById(R.id.contentListViewLeft);
        contentListViewRight = (MyListView) contentView.findViewById(R.id.contentListViewRight);

        tvItem = (TextView) contentView.findViewById(R.id.tv_item);
//        OverScrollDecoratorHelper.setUpOverScroll(rightTitleHorscrollView);
//        OverScrollDecoratorHelper.setUpOverScroll(rightContentHorscrollView);
        //ListView关联ScrollView
        rightTitleHorscrollView.setmSyncView(rightContentHorscrollView);
        rightContentHorscrollView.setmSyncView(rightTitleHorscrollView);
        appraisersLeftAdapter = new AppraisersLeftAdapter(this, AppraisersDatas);
        appraisersRightAdapter = new AppraisersRightAdapter(this, AppraisersDatas);
        contentListViewLeft.setAdapter(appraisersLeftAdapter);
        contentListViewRight.setAdapter(appraisersRightAdapter);

        tv_timestart = (TextView) foot.findViewById(R.id.tv_timestart);
        tv_timeend = (TextView) foot.findViewById(R.id.tv_timeend);
        //时间
        datelistview = new ListView(this);
        datelistview.setDividerHeight(0);
        datelistview.addFooterView(foot);
        popAdapter = new PopAdapter(date, appContext);
        datelistview.setAdapter(popAdapter);
//        popAdapter.setCheckItem(2);
//        dropDownMenu.setTabText(date[2]);
        //城市
        cityListView.setDividerHeight(0);
        cityPopAdapter = new CityPopAdapter(citys, appContext);
        cityListView.setAdapter(cityPopAdapter);

        listViews.add(datelistview);
        listViews.add(cityView);
        dropDownMenu.setDropDownMenu(Arrays.asList(titles), listViews, contentView);
    }

//    public void onScrollChanged(int l, int t, int oldl, int oldt) {
//        for (AppraisersStatisticsCHScrollView2 scrollView : mHScrollViews) {
//            //防止重复滑动
//            if (mTouchView != scrollView)
//                scrollView.smoothScrollTo(l, t);
//        }
//    }

    private void choosStartDate(int requestCode) {
        Intent intent = new Intent(this, SelectDateActivity1.class);

        if (!TextUtils.isEmpty(endDate)) {
            intent.putExtra("opYear", StringUtils.getYear4String(endDate));
            intent.putExtra("opMonth", StringUtils.getMonth4String(endDate));
            intent.putExtra("opDay", StringUtils.getDay4String(endDate));
        }
        intent.putExtra(Constant.KEY_TITLE, "");
        startActivityForResult(intent, requestCode);
    }

    private void choosEndDate(int requestCode) {
        Intent intent = new Intent(this, SelectDateActivity1.class);
        if (!TextUtils.isEmpty(startDate)) {
            intent.putExtra("Minyear", StringUtils.getYear4String(startDate));
            intent.putExtra("MinMonth", StringUtils.getMonth4String(startDate));
            intent.putExtra("opDay", StringUtils.getDay4String(startDate));
            intent.putExtra("isEnd", true);
        }
        intent.putExtra(Constant.KEY_TITLE, "");
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case Constant.TIME_START:
                startDate = data.getStringExtra("car_Time_MSG");
                tv_timestart.setText(startDate);
                BeginDate = startDate;
                clossTimeDownMenu();
                break;
            case Constant.TIME_END:
                endDate = data.getStringExtra("car_Time_MSG");
                EndDate = endDate;
                tv_timeend.setText(endDate);
                clossTimeDownMenu();
                break;
            default:

                break;
        }
    }

    private void clossTimeDownMenu() {
        popAdapter.setCheckItem(-1);
        if (!TextUtils.isEmpty(startDate)) {
            dropDownMenu.setTabText(startDate);
            if (!TextUtils.isEmpty(endDate)) {
                dropDownMenu.setTabText(startDate + "-" + endDate);
            }
        } else {
            if (!TextUtils.isEmpty(endDate)) {
                dropDownMenu.setTabText(endDate);
            }
        }
        if (!TextUtils.isEmpty(startDate) && !TextUtils.isEmpty(endDate)) {
            if (StringUtils.TimeCompare(endDate, startDate)) {
                dropDownMenu.closeMenu();
                showDialog();
                appraisersPresenter.getAppraisersData(getappraisersDataParams());
            } else {
                tv_timeend.setText("");
                MyToast.showShort(Constant.SHOWTIMETOAST);
            }
        }

    }

    private Map<String, String> getCityDataParams() {
        Map<String, String> params = new HashMap<>();
        params.put("UserID", user.getUserId() + "");
        return params;
    }

    private Map<String, String> getappraisersDataParams() {
        Map<String, String> params = new HashMap<>();
        params.put("tokenid", "6");
        params.put("UserID", user.getUserId() + "");
        if (!TextUtils.isEmpty(CityID)) {
            params.put("CityID", CityID);
        }
        if (!TextUtils.isEmpty(BeginDate) && !TextUtils.isEmpty(EndDate)) {
            params.put("DateType", "5");
            params.put("BeginDate", BeginDate);
            params.put("EndDate", EndDate);
        } else {
            params.put("DateType", DateType);
        }
        Map<String, Object> signMaps = new HashMap<>();
        signMaps.putAll(params);
        params.put("sign", MD5Utils.getMD5Sign(signMaps));
        Logger.e("评估师统计参数----" + params.toString());
        return params;
    }

    //大区城市返回数据
    @Override
    public void cityDataSuccess(CityItem cityItem) {
        dismissDialog();
        if (cityItem.getStatus() == 100) {
            citys.addAll(cityItem.getData());
//        cityPopAdapter.notifyDataSetChanged();
            CityID = citys.get(0).getCityId() + "";
            titles[1] = citys.get(0).getCityName();
            initView();
            initListtener();
            initData();
        }


    }

    @Override
    public void appraisersDataSuccess(AppraisersData appraisersData) {
        dismissDialog();
        AppraisersDatas.clear();
        if (appraisersData.getStatus() == 100) {
            if (appraisersData.getData() != null) {
                AppraisersDatas.addAll(appraisersData.getData());
                appraisersLeftAdapter.notifyDataSetChanged();
                appraisersRightAdapter.notifyDataSetChanged();
            }

        } else {
            MyToast.showShort(appraisersData.getMsg());
        }
    }

    @Override
    public void showError(String error) {
        super.showError(error);
        dismissDialog();
    }


}
